package com.hzvdvmc.autosale.setting.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.hzvdvmc.autosale.R;
import com.hzvdvmc.autosale.base.ActionbarActivity;
import com.hzvdvmc.autosale.common.OnItemClickListener;
import com.hzvdvmc.autosale.common.OnItemLongClickListener;
import com.hzvdvmc.autosale.setting.adapter.LogFileRvAdapter;
import com.hzvdvmc.autosale.util.LogToFileHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LogFileActivity extends ActionbarActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_func)
    TextView tvFunc;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    //日志保存最大天数
    private static final int MAX_LOG_DAY = 30;
    private List<File> fileList = new ArrayList<>();
    private LogFileRvAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);
        ButterKnife.bind(this);
        setCustomeActiorBar("查看日志");

        initView();
        initData();
    }

    private void initView() {
        mAdapter = new LogFileRvAdapter(fileList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                LogContentActivity.startMe(LogFileActivity.this,
                        fileList.get(position).getName(),
                        fileList.get(position).getAbsolutePath());
            }
        });
        mAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public void onLongClick(View v, int position) {
                showDeleteDialog(position);
            }
        });

    }


    private void showDeleteDialog(final int position) {
        final String[] items = {"删除"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                File file = fileList.remove(position);
                if (file.exists()) {
                    file.delete();
                    initData();
                }

            }
        });
        builder.create().show();
    }

    private void initData() {
        fileList.clear();
        File file = new File(LogToFileHelper.getLogPath());
        if (file != null && file.exists()) {
            File[] files = file.listFiles();

            Arrays.sort(files, new Comparator<File>() {
                public int compare(File f1, File f2) {
                    long diff = f1.lastModified() - f2.lastModified();
                    if (diff > 0)
                        return 1;
                    else if (diff == 0)
                        return 0;
                    else
                        return -1;
                }

                public boolean equals(Object obj) {
                    return true;
                }
            });
            fileList.addAll(Arrays.asList(files));
        }
        //删除过期日志
        while (fileList.size() > MAX_LOG_DAY) {
            File remove = fileList.remove(0);
            if (remove.exists()) {
                remove.delete();
            }
        }
        mAdapter.notifyDataSetChanged();
    }
}
